﻿// <copyright file="ListControlExtensions.cs" company="Edge Extensions Project">
// Copyright (c) 2009 All Rights Reserved
// </copyright>
// <author>Kevin Nessland</author>
// <email>kevinnessland@gmail.com</email>
// <date>2009-07-08</date>
// <summary>Contains ListControl-related extension methods.</summary>
namespace Edge.Extensions.Web
{
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Provides extension methods to the <see cref="ListControl">ListControl</see> object.
    /// </summary>
    public static class ListControlExtensions
    {
        /// <summary>
        /// Adds a new item to the <see cref="ListControl">ListControl</see>.
        /// </summary>
        /// <param name="listControl">The list control to manipulate.</param>
        /// <param name="text">The text value for the new list item.</param>
        /// <param name="value">The value for the new list item.</param>
        public static void AddItem(this ListControl listControl, string text, string value)
        {
            AddItem(listControl, text, value, false);
        }

        /// <summary>
        /// Adds a new item to the <see cref="ListControl">ListControl</see> and optionally allows for the item to be immediately selected.
        /// </summary>
        /// <param name="listControl">The list control to manipulate.</param>
        /// <param name="text">The text value for the new list item.</param>
        /// <param name="value">The value for the new list item.</param>
        /// <param name="selected">If set to <c>true</c> preselect the new list item.</param>
        /// <remarks>If <paramref name="selected">selected</paramref> equals <c>true</c>, any other list item in the <see cref="ListControl">ListControl</see> will be unselected.</remarks>
        public static void AddItem(this ListControl listControl, string text, string value, bool selected)
        {
            ListItem item = new ListItem(text, value);

            if (selected == true)
            {
                foreach (ListItem itemCurrent in listControl.Items)
                {
                    itemCurrent.Selected = false;
                }

                item.Selected = true;
            }
            else
            {
                item.Selected = false;
            }

            listControl.Items.Add(item);
        }

        /// <summary>
        /// Sorts the <see cref="ListControl">ListControl</see> by its <see cref="ListControl.Text">Text</see> property.
        /// </summary>
        /// <param name="value">The <see cref="ListControl">ListControl</see> to be sorted.</param>
        public static void SortByText(this ListControl value)
        {
            ArrayList aylDropDownListItems = new ArrayList();
            int intListItem = 0;
            string[] arrListItem = null;
            string strSelectedValue = value.SelectedValue;
            ListItem objListItem = default(ListItem);

            for (intListItem = 0; intListItem <= value.Items.Count - 1; intListItem++)
            {
                aylDropDownListItems.Add(value.Items[intListItem].Text.ToString() + "|" + value.Items[intListItem].Value.ToString());
            }

            aylDropDownListItems.Sort();

            value.Items.Clear();

            for (intListItem = 0; intListItem <= aylDropDownListItems.Count - 1; intListItem++)
            {
                arrListItem = aylDropDownListItems[intListItem].ToString().Split(Convert.ToChar("|"));
                objListItem = new ListItem(arrListItem[0], arrListItem[1]);
                if (objListItem.Value == strSelectedValue)
                {
                    objListItem.Selected = true;
                }

                value.Items.Add(objListItem);
            }
        }

        /// <summary>
        /// Sorts the <see cref="ListControl">ListControl</see> by its <see cref="ListControl.Text">Value</see> property.
        /// </summary>
        /// <param name="value">The <see cref="ListControl">ListControl</see> to be sorted.</param>
        public static void SortByValue(this ListControl value)
        {
            ArrayList aylDropDownListItems = new ArrayList();

            int intListItem = 0;

            string[] arrListItem = null;

            for (intListItem = 0; intListItem <= value.Items.Count - 1; intListItem++)
            {
                aylDropDownListItems.Add(value.Items[intListItem].Value.ToString() + "|" + value.Items[intListItem].Text.ToString());
            }

            aylDropDownListItems.Sort();

            value.Items.Clear();

            for (intListItem = 0; intListItem <= aylDropDownListItems.Count - 1; intListItem++)
            {
                arrListItem = aylDropDownListItems[intListItem].ToString().Split(Convert.ToChar("|"));

                value.Items.Add(new ListItem(arrListItem[1], arrListItem[0]));
            }
        }

        /// <summary>
        /// Gets a string array representing the values from the checked items in the <see cref="ListControl">ListControl</see>.
        /// </summary>
        /// <param name="value">The <see cref="ListControl">ListControl</see> to retrieve from.</param>
        /// <returns>An array of <see cref="String">String</see> containing the values from checked items within the specified <see cref="ListControl">ListControl</see>.</returns>
        public static string[] ToArray(this ListControl value)
        {
            string[] returnValue = null;

            ArrayList list = new ArrayList();

            foreach (ListItem item in value.Items)
            {
                if (item.Selected == true)
                {
                    list.Add(item.Value);
                }
            }

            if (list.Count > 0)
            {
                for (int i = 0; i <= list.Count - 1; i++)
                {
                    returnValue[i] = list[i].ToString();
                }
            }

            return returnValue;
        }
    }
}
